package com.yun.utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;

public class ValidateUtility {
	

	public static  HashMap  validate(BindingResult result) {
		if (result.hasErrors()) {
			Map<String, Object> map = new HashMap<String, Object>();
			List<FieldError> fieldErrors = result.getFieldErrors();
			for (FieldError error : fieldErrors) {
				// 属性名
				String filedNaString = error.getField();
				// 错误 信息
				String defaultMessage = error.getDefaultMessage();
				map.put(filedNaString, defaultMessage);
			}
			return ServerResponse.build("400", "参数有误", map);
		}
		return ServerResponse.build("200", "参数正确");
	}
	
	
	
	
	public static  HashMap  validate(BindingResult result,FieldError fieldError) {
		if (result.hasErrors()) {
			Map<String, Object> map = new HashMap<String, Object>();
			List<FieldError> error = result.getFieldErrors(fieldError.getField());
		        for(FieldError error2:error) {
				// 属性名
				String filedNaString = error2.getField();
				// 错误 信息
				String defaultMessage = error2.getDefaultMessage();
				map.put(filedNaString, defaultMessage);
		        }
			return ServerResponse.build("400", "参数有误", map);
		}
		return ServerResponse.build("200", "参数正确");
	}
}
